import { getTarget } from '@/services';
import ProTable, { ProColumns } from '@ant-design/pro-table';
import { useParams, history } from 'umi';
import { TargetType } from '@/type';
import { message } from 'antd';

type WithoutFlid = Omit<TargetType, 'flid'> & { sId?: string };

export default () => {
  const params = useParams<{ zb: string }>();

  const query = async () => {
    const data = await getTarget({ flid: params.zb });
    if (data.msg === '失败') {
      message.error(data.content);
      return {
        data: []
      }
    }
    return { data: data.content };
  };

  const columns: ProColumns<WithoutFlid>[] = [
    {
      dataIndex: 'id',
      title: 'id',
    },
    {
      dataIndex: 'name',
      title: '名字',
    },
    {
      dataIndex: 'ps',
      title: '描述',
    },
    {
      dataIndex: 'gs',
      title: '公式',
    },
    {
      title: '操作',
      valueType: 'option',
      render: (text, record, _, action) => [
        <a
          key="editable"
          onClick={() => {
            history.push({
              pathname: `/search/fl/${params.zb}/${record.id}`,
              state: {
                gs: record.gs,
                name: record.name
              }
            });
            
          }}
        >
          查看趋势图
        </a>,
      ],
    },
  ];

  return (
    <ProTable<WithoutFlid>
      rowKey="id"
      headerTitle="指标"
      columns={columns}
      request={query}
      search={false}
      pagination={false}
      options={false}
    />
  );
};
